<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------


/**
 * 会员操作功能
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   create by allen.qiang
 */

quanli_require('module/base/customer/quanliCust.class.php');
quanli_require('module/base/system/itemHandler.class.php');
quanli_require('module/base/customer/custRank.class.php');

class pa_customer{
	
	public function execute() {
		
		if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])) {

			switch ( $_REQUEST['operFlg'] ) {

				case 1: //会员查询
					$this->custList();
					break;

                case 2: //新增会员
                case 3: //修改会员
                    $this->custEdit( $_REQUEST['operFlg'] );
                    break;

                case 10://会员信息变更日志列表
                    $this->customerLogDetail();
                    break;

                case 12://会员账户
                    $this->customerAccount();
                    break;

                default:
                    backendError();
                    break;

			}

		} else {

            backendError();

        }

	}
	
	/**
	 * 会员初始化查询
	 */
	private function custList() {

		global $G_PGLOB;

        //加载会员等级类
        quanli_include('module/base/customer/custRank.class.php');

        //实例会员等级类
        $custRank = new custRank();

        //查询会员等级
        $args_array = array();
        $rank_array = $custRank->searchRank( $args_array );
		
        $G_PGLOB->smarty->assign('rank_array', $rank_array);
        $G_PGLOB->smarty->assign('edit_permission', $G_PGLOB->has_permission(3001));

		$G_PGLOB->smarty->display('customer/customer.tpl');

	}
	
    /**
     * 新增、修改会员
     * @param int $operFlg 2、新增 3、修改
     */
    private function custEdit( $operFlg ) {

        global $G_PGLOB;

        if ($operFlg == 3) {

            if (!isset($_REQUEST['pcustID']) || !quanli_is_int($_REQUEST['pcustID'])) {
                backendError();
            }

            //实例会员类
            $quanliCust = new quanliCust();

            //查询会员信息
            $customer_array = $quanliCust->searchCustomerDetail( $_REQUEST );
            if (!$customer_array) {
                backendError('会员信息没有找到');
            }

            $G_PGLOB->smarty->assign_by_ref('customer_array', $customer_array);

        }
        if( isset($_REQUEST['mobile']) ){
            $G_PGLOB->smarty->assign('mobile',$_REQUEST['mobile'] );
        }
        $G_PGLOB->smarty->display('customer/customerEdit.tpl');

    }

    //会员信息变更日志列表
    function customerLogDetail(){
        global $G_PGLOB;

        if (!isset($_REQUEST['pcustID']) || !is_numeric($_REQUEST['pcustID'])) {
            $G_PGLOB->smarty->assign('msg', '参数错误');
            $G_PGLOB->smarty->display('comm/error.tpl');
            die() ;
        }

        $G_PGLOB->smarty->assign('pcustID' , $_REQUEST['pcustID']);
        $G_PGLOB->smarty->display('customer/customerLog.tpl');
    }

    //会员账户
    function customerAccount(){

        global $G_PGLOB;

        quanli_require('module/func/eai.func.php');

        if (!isset($_REQUEST['pcustID']) || !quanli_is_int($_REQUEST['pcustID'])) {
            backendError();
        }

        //实例会员类
        $quanliCust = new quanliCust();

        //查询会员信息
        $customer_array = $quanliCust->searchCustomerDetail( $_REQUEST );
        if( empty($customer_array) ){
            backendError('会员信息没有找到');
        }
        $G_PGLOB->smarty->assign_by_ref('customerInfo' , $customer_array);

        //实例crm类
        $crm_obj = eai_crm();

        //查询会员账户
        $account_array = $crm_obj->searchCustAllAccount($_REQUEST['pcustID']);
        if ($account_array['code'] == 200) {
            $G_PGLOB->smarty->assign_by_ref('account_array' , $account_array['data']);
        }

        $G_PGLOB->smarty->display('customer/customerAccount.tpl');

    }

}